package me.zfz.business.controller.impl;

import com.mixsmart.utils.StringUtils;
import me.zfz.base.bean.SmartResponse;
import me.zfz.base.constant.IConstant;
import me.zfz.base.plugins.OrgUserZTreeData;
import me.zfz.base.tag.bean.*;
import me.zfz.business.bean.RequestPage;
import me.zfz.business.bean.UserSearchParam;
import me.zfz.business.bean.entity.StudentInfo;
import me.zfz.business.bean.entity.TNUser;
import me.zfz.business.constant.IWebConstant;
import me.zfz.business.constant.enums.BtnPropType;
import me.zfz.business.constant.enums.SelectedEventType;
import me.zfz.business.controller.base.BaseController;
import me.zfz.business.service.OPService;
import me.zfz.business.service.StudentInfoService;
import me.zfz.business.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * 用户
 *
 * @author author
 */
@Controller
@RequestMapping("/stu")
public class StudentInfoController extends BaseController {

    private static final String VIEW_DIR = IWebConstant.WEB_BASE_VIEW_DIR + "/stu";

    @Autowired
    private StudentInfoService studentInfoService;
    @Autowired
    private OPService opServ;

    @RequestMapping("/list")
    public ModelAndView list(HttpSession session, UserSearchParam searchParam, ModelAndView modelView, RequestPage page) throws Exception {
        String uri = "stu/list";
//        addBtn = new EditBtn("add", "showPage/base_stu_add", null, "添加学生信息", "900");
        editBtn = new EditBtn("edit", "showPage/base_stu_edit", "stu", "修改学生信息", "900");
//        delBtn = new DelBtn("user/delete", "确定要删除选中的学生吗？", uri, null, null);
        refreshBtn = new RefreshBtn(uri, "resource", null);

        if ("0".equals(searchParam.getOrgId())) {
            searchParam.setOrgId(null);
        }
        List<String> orgIds = getUserInfoFromSession(session).getOrgIds();
        orgIds = orgIds.contains("0") ? null : orgIds;
        searchParam.setOrgIds(StringUtils.list2Array(orgIds));
        pageParam = new PageParam(uri, null, page.getPage(), page.getPageSize());

        SmartResponse<Object> smartResp = studentInfoService.findAllObj(searchParam, page.getStartNum(), page.getPageSize());
        ModelMap modelMap = modelView.getModelMap();
        modelMap.put("smartResp", smartResp);
        modelMap.put("addBtn", addBtn);
        modelMap.put("editBtn", editBtn);
        modelMap.put("delBtn", delBtn);
        modelMap.put("refreshBtn", refreshBtn);
        modelMap.put("pageParam", pageParam);
        modelMap.put("searchParam", searchParam);
        modelMap.put("customBtns", customBtns);
        modelView.setViewName(VIEW_DIR + "/list");
        return modelView;
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public @ResponseBody
    SmartResponse<String> add(StudentInfo studentInfo) throws Exception {
        SmartResponse<String> smartResp = new SmartResponse<String>();
        if (null != studentInfo) {
            smartResp = studentInfoService.save(studentInfo);
        }
        return smartResp;
    }

    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public @ResponseBody
    SmartResponse<String> edit(StudentInfo studentInfo) throws Exception {
        SmartResponse<String> smartResp = new SmartResponse<String>();
        if (null != studentInfo) {
            smartResp = studentInfoService.update(studentInfo);
        }
        return smartResp;
    }

    /**
     * 删除用户信息
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public SmartResponse<String> delete(String id) {
        return studentInfoService.delete(id);
    }


//    /**
//     * 简单列表
//     *
//     * @return
//     * @throws Exception
//     */
//    @RequestMapping("/simplist")
//    public ModelAndView simplist(HttpSession session, UserSearchParam searchParam,
//                                 ModelAndView modelView, RequestPage page) throws Exception {
//        String uri = "stu/simplist";
//        searchParam.setOrgIds(StringUtils.list2Array(getUserInfoFromSession(session).getOrgIds()));
//        SmartResponse<Object> smartResp = opServ.getDatas("stu_simp_list", searchParam, page.getStartNum(), page.getPageSize());
//        pageParam = new PageParam(uri, "#user-tab", page.getPage(), page.getPageSize());
//        selectedEventProp = new SelectedEventProp(SelectedEventType.OPEN_TO_TARGET.getValue(), "auth/userHas", "#has-auth-list", "id");
//
//        ModelMap modelMap = modelView.getModelMap();
//        modelMap.put("smartResp", smartResp);
//        modelMap.put("pageParam", pageParam);
//        modelMap.put("searchParam", searchParam);
//        modelMap.put("selectedEventProp", selectedEventProp);
//        pageParam = null;
//
//        modelView.setViewName(VIEW_DIR + "/simplist");
//        return modelView;
//    }

    @RequestMapping("/detail")
    public ModelAndView userInfo(ModelAndView modelView, String id) throws Exception {
        SmartResponse<Object> smartResp = new SmartResponse<Object>();
        if (StringUtils.isNotEmpty(id)) {
            smartResp = studentInfoService.find(StudentInfo.class, id);
        }
        modelView.getModelMap().put("smartResp", smartResp);
        modelView.setViewName(VIEW_DIR + "/detail");
        return modelView;
    }

}
